using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;

public enum Sound
{
    HitNail,
    Slot1,
    Slot5,
}

public class GameManager : MonoBehaviour
{
    private static GameManager _instance;
    public static GameManager Instance => _instance;
    
    public Text txtScore;
    public PachinkoBall ball;

    public AudioClip clipNail;
    public AudioClip clipSlot1;
    public AudioClip clipSlot5;

    public Text txtLHit5;
    public Text txtLHit1;
    public Text txtRHit5;
    public Text txtRHit1;
    
    private int score;
    private int totalHit;
    private int _LHit5;
    private int _LHit1;
    private int _RHit5;
    private int _RHit1;
    // private int _Missed;
    
    // Start is called before the first frame update
    void Awake()
    {
        _instance = this;
        
        score = 0;
        totalHit = 0;
        _LHit5 = 0;
        _LHit1 = 0;
        _RHit5 = 0;
        _RHit1 = 0;

        UpdateScore();
    }


    public void Retry()
    {
        // totalHit++;
        // txtScore.text = string.Format("{0}/{1}hit", score, totalHit);
        
        ball.Restart();
    }

    public void FallInSlot(GameObject slot)
    {
        Animator anim = slot.GetComponent<Animator>();
        anim.SetTrigger("hit");
        
        string name = slot.name;
        int score = int.Parse(name.Substring(name.Length - 1));

        if (score == 1)
        {
            if (name.Contains("L"))
                _LHit1++;
            else
                _RHit1++;
            
            PlaySound(Sound.Slot1);
        }
        else if (score == 5)
        {
            if (name.Contains("L"))
                _LHit5++;
            else
                _RHit5++;
            
            PlaySound(Sound.Slot5);
        }
        
        AddScore(score);
    }

    void AddScore(int value)
    {
        score += value;
        totalHit++;
        
        UpdateScore();
    }

    void UpdateScore()
    {
        txtScore.text = string.Format("{0}/{1}hit", score, totalHit);
        txtLHit5.text = string.Format("L5Hit:{0}({1}%)", _LHit5, totalHit == 0 ? "0" : ((float)_LHit5*100/totalHit).ToString("F2"));
        txtLHit1.text = string.Format("L1Hit:{0}({1}%)", _LHit1, totalHit == 0 ? "0" : ((float)_LHit1*100/totalHit).ToString("F2"));
        txtRHit5.text = string.Format("R5Hit:{0}({1}%)", _RHit5, totalHit == 0 ? "0" : ((float)_RHit5*100/totalHit).ToString("F2"));
        txtRHit1.text = string.Format("R1Hit:{0}({1}%)", _RHit1, totalHit == 0 ? "0" : ((float)_RHit1*100/totalHit).ToString("F2"));
    }

    /// <summary>
    /// 计算命中结果
    /// </summary>
    /// <returns>true:向右弹跳，false:向左弹跳</returns>
    public bool RandomHit()
    {
        int rnd = Random.Range(0, 2);
        return rnd == 1;
    }

    public void PlaySound(Sound soundClip)
    {
        AudioClip clip;
        switch (soundClip)
        {
            case Sound.HitNail:
                clip = clipNail;
                break;
            case Sound.Slot1:
                clip = clipSlot1;
                break;
            case Sound.Slot5:
                clip = clipSlot5;
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(clip), soundClip, null);
        }
        
        AudioSource.PlayClipAtPoint(clip, Vector3.zero);
    }
}
